/*
 * OutputListener.java
 */

package xq.ide;

import java.io.PrintWriter;
import java.io.StringWriter;
import xq.api.XQException;
import xq.api.XQRequest;
import xq.api.XQResponse;

/**
 *
 * @author ThorntonRP
 */
public abstract class XQOutputHandler {

	public abstract void printMessage(OutputLevel level, String message);
	
	public void printInfo(String message) {
		printMessage(OutputLevel.INFORMATION, message);
	}
	
	public void printWarning(String message) {
		printMessage(OutputLevel.WARNING, message);
	}
	
	public void printError(String message) {
		printMessage(OutputLevel.ERROR, message);
	}
	
	public void printException(Exception thrown) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		if (thrown instanceof XQException) {
			pw.print(thrown.getMessage());
		} else {
			thrown.printStackTrace(pw);
		}
		printError(sw.toString());
	}
	
	public void printResponse(XQRequest request, XQResponse<String> response) {
		String result = XQUtil.format(response.getResult());
		printMessage(OutputLevel.INFORMATION, result);
	}
}
